﻿var AppShoppingCart = {
    ShoppingCartAmount: 0,
    JsonList: {},
    UpdateList: function (productId, buyCount) {//更新列表 和总价格
        var amount = 0.00;
        var jsonTemp = AppShoppingCart.JsonList;
        AppShoppingCart.JsonList = new Array();
        for (var i = 0; i < jsonTemp.length; i++) {
            if (jsonTemp[i].ProductId == productId) {
                if (buyCount == 0) {
                    continue;
                }
                jsonTemp[i].BuyCount = buyCount;
            }
            AppShoppingCart.JsonList.push(jsonTemp[i]);
            if (jsonTemp[i].Price != null) {
                amount += (jsonTemp[i].Price * jsonTemp[i].BuyCount);
            }
        }
        AppShoppingCart.ShoppingCartAmount = amount;
        $("#PriceShow").html("¥ " + amount.toFixed(2));
    },
    PrivateAddCoooke: function (url, p, offset) {//更新数量 和 Cookies
        var productId = p.attr("id");
        var count = parseFloat(p.find("input").val());
        if (count == NaN) {
            count = 1;
        }
        var hasDel = false;
        if ((offset == -1 && count == 1)||count==0)
            hasDel=true;
        var totalCount = offset + count;
        $.get(url, { r: Math.random(), productId: productId, buyCount: totalCount }, function () {
            p.find("input").val(totalCount);
            AppShoppingCart.UpdateList(productId, totalCount);
            if (hasDel) {
                var m =p.parent();
                var mp = m.parent();
                m.remove();
                if (mp.find(".tablebody").size() == 0) {
                    mp.remove();
                }
            }
        });
        return false;
    },
    MinusOrAdd: function (url) {
        //减或者加 数量
        $(".acount").click(function () {
            var $p = $(this).parent();
            var offset = parseFloat($(this).attr("ref"));
            return AppShoppingCart.PrivateAddCoooke(url, $p, offset);
        });
        $(".BuyCountText").blur(function () {
            var $p = $(this).parent();
            return AppShoppingCart.PrivateAddCoooke(url, $p, 0);
        });
        //删除
        $(".Del").click(function () {
            var $ptemp = $(this);
            var productId = $ptemp.attr("ref");
            $.get(url, { r: Math.random(), productId: productId, buyCount: 0 }, function () {
                AppShoppingCart.UpdateList(productId, 0);
                var m = $ptemp.parent().parent();
                var mp = m.parent();
                m.remove();
                if (mp.find(".tablebody").size() == 0) {
                    mp.remove();
                }
            });
        });
    }
};

$(function () {
    var url = G_Url + 'Products/Order/ShoppingCartUpdate';
    $.get(G_Url + 'Products/Order/GetShoppingCart', { r: Math.random() }, function (d) {
        var jsonList = CallBackObject(d);
        AppShoppingCart.JsonList = jsonList;
    });
    AppShoppingCart.MinusOrAdd(url);
});